package com.springboot.jeesite.modules.sys.web;


import com.springboot.jeesite.modules.sys.entity.UserOnline;
import com.springboot.jeesite.modules.sys.service.SessionService;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.List;

@RequestMapping("${adminPath}/sys/online")
@Controller
public class SessionController {
    @Autowired
    SessionService sessionService;

    @RequestMapping("/list")
    public String list(UserOnline userOnline, Model model) {
        List<UserOnline> userOnlines = sessionService.list(userOnline.getUsername());
        model.addAttribute("userOnlines", userOnlines);
        model.addAttribute("userOnline", userOnline);
        return "modules/sys/userOnlineList";
    }

    @RequestMapping("/forceLogout")
    public String forceLogout(String sessionId, Model model) {
        try {
            sessionService.forceLogout(sessionId);
            List<UserOnline> userOnlines = sessionService.list(null);
            model.addAttribute("userOnlines", userOnlines);
            model.addAttribute("userOnline", new UserOnline());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "modules/sys/userOnlineList";
    }

    @RequestMapping("/sessionList")
    public Collection<Session> sessionList() {
        return sessionService.sessionList();
    }


}
